package ch.exploadingelephants.graph;

import java.util.List;

import ch.exploadingelephants.model.ConveyorDirection;

/**
 * @author Sandro De Zanet
 * 
 */
public class YGBifurcationToken extends Token {

    Token yellow, green, empty;
    private final ConveyorDirection direction;

    /**
     * 
     */
    public YGBifurcationToken(ConveyorDirection direction) {
	this.direction = direction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#accept(ch.exploadingelephants.graph
     * .DotString)
     */
    @Override
    public TokenResult accept(DotString inputPattern,
	    List<VisitedNodeState> visited) throws InfinityLoopException {
	checkForInfiniteLoop(inputPattern, visited);
	DotColor next = inputPattern.next();
	DotString newPattern = inputPattern.consumate();
	if (next == null) {
	    return empty.accept(newPattern, visited);
	} else if (next == DotColor.YELLOW) {
	    return yellow.accept(newPattern, visited);
	} else if (next == DotColor.GREEN) {
	    return green.accept(newPattern, visited);
	} else
	    return empty.accept(inputPattern.copy(), visited);
    }

    public void setYellow(Token yellow) {
	this.yellow = yellow;
    }

    public void setGreen(Token green) {
	this.green = green;
    }

    public void setEmpty(Token empty) {
	this.empty = empty;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#tryConnect(ch.exploadingelephants.
     * graph.Token, ch.exploadingelephants.model.ConveyorDirection)
     */
    @Override
    public boolean tryConnect(Token createGraphObject,
	    ConveyorDirection direction) {
	if (direction.equals(this.direction)) {
	    setEmpty(createGraphObject);
	    return true;
	} else if (this.direction.turnLeft().equals(direction)) {
	    setYellow(createGraphObject);
	    return true;
	} else if (this.direction.turnRight().equals(direction)) {
	    setGreen(createGraphObject);
	    return true;
	}
	return false;

    }
}
